/**
 * PLM项目流程服务
 * 提供流程相关的服务功能
 */

// 流程状态常量
const FLOW_STATUS = {
  DRAFT: 0,     // 草稿
  PENDING: 1,   // 审批中
  APPROVED: 2,  // 已批准
  REJECTED: 3,  // 已拒绝
  CANCELED: 4   // 已取消
};

// 流程类型常量
const FLOW_TYPES = {
  PROJECT_APPROVAL: 'project_approval',  // 项目立项
  DESIGN_APPROVAL: 'design_approval',    // 设计审批
  CHANGE_REQUEST: 'change_request',      // 变更申请
  TEST_REPORT: 'test_report'             // 测试报告
};

// 流程服务
const flowService = {
  /**
   * 获取流程列表
   * @param {Object} params 查询参数
   * @returns {Promise<Object>} 流程列表
   */
  getFlowList: async function(params = {}) {
    console.log('调用flowService.getFlowList', params);
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        list: [
          {
            _id: 'flow001',
            name: '产品A立项申请',
            type: FLOW_TYPES.PROJECT_APPROVAL,
            status: FLOW_STATUS.APPROVED,
            creator: '张三',
            createTime: Date.now() - 86400000 * 15,
            approver: '王总',
            approveTime: Date.now() - 86400000 * 12
          },
          {
            _id: 'flow002',
            name: '产品B设计变更',
            type: FLOW_TYPES.CHANGE_REQUEST,
            status: FLOW_STATUS.PENDING,
            creator: '李四',
            createTime: Date.now() - 86400000 * 5,
            currentApprover: '技术总监'
          }
        ],
        total: 2
      }
    };
  },
  
  /**
   * 获取流程详情
   * @param {string} id 流程ID
   * @returns {Promise<Object>} 流程详情
   */
  getFlowDetail: async function(id) {
    console.log('调用flowService.getFlowDetail', id);
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        _id: id,
        name: '模拟流程' + id,
        type: FLOW_TYPES.PROJECT_APPROVAL,
        status: FLOW_STATUS.PENDING,
        creator: '张三',
        createTime: Date.now() - 86400000 * 5,
        currentApprover: '总经理',
        description: '这是一个模拟流程，用于测试',
        history: [
          {
            node: '发起申请',
            operator: '张三',
            time: Date.now() - 86400000 * 5,
            comment: '申请立项'
          },
          {
            node: '部门审批',
            operator: '李经理',
            time: Date.now() - 86400000 * 4,
            comment: '部门同意'
          }
        ],
        form: {
          title: '项目立项申请',
          budget: 100000,
          startDate: Date.now(),
          endDate: Date.now() + 86400000 * 90,
          reason: '市场需求分析表明该产品有较大市场空间',
          risk: '技术风险较低，市场风险中等'
        }
      }
    };
  },
  
  /**
   * 创建流程
   * @param {Object} data 流程数据
   * @returns {Promise<Object>} 创建结果
   */
  createFlow: async function(data) {
    console.log('调用flowService.createFlow', data);
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        _id: 'flow' + Date.now().toString().substring(8),
        message: '流程创建成功'
      }
    };
  },
  
  /**
   * 流程审批
   * @param {string} id 流程ID
   * @param {boolean} approved 是否批准
   * @param {string} comment 审批意见
   * @returns {Promise<Object>} 审批结果
   */
  approveFlow: async function(id, approved, comment) {
    console.log('调用flowService.approveFlow', { id, approved, comment });
    
    // 返回模拟数据
    return {
      code: 0,
      data: {
        message: approved ? '流程已批准' : '流程已拒绝',
        status: approved ? FLOW_STATUS.APPROVED : FLOW_STATUS.REJECTED
      }
    };
  },
  
  /**
   * 获取流程状态文本
   * @param {number} status 状态码
   * @returns {string} 状态文本
   */
  getStatusText: function(status) {
    const statusMap = {
      [FLOW_STATUS.DRAFT]: '草稿',
      [FLOW_STATUS.PENDING]: '审批中',
      [FLOW_STATUS.APPROVED]: '已批准',
      [FLOW_STATUS.REJECTED]: '已拒绝',
      [FLOW_STATUS.CANCELED]: '已取消'
    };
    
    return statusMap[status] || '未知状态';
  },
  
  /**
   * 获取流程类型文本
   * @param {string} type 类型标识
   * @returns {string} 类型文本
   */
  getTypeText: function(type) {
    const typeMap = {
      [FLOW_TYPES.PROJECT_APPROVAL]: '项目立项',
      [FLOW_TYPES.DESIGN_APPROVAL]: '设计审批',
      [FLOW_TYPES.CHANGE_REQUEST]: '变更申请',
      [FLOW_TYPES.TEST_REPORT]: '测试报告'
    };
    
    return typeMap[type] || '未知类型';
  }
};

// 导出流程服务常量和方法
module.exports = {
  ...flowService,
  FLOW_STATUS,
  FLOW_TYPES
}; 